package com.kaiwukj.android.ufamily.mvp.ui.page.dynamic.detail;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.TimeUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.CommentResult;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class DynamicCommentAdapter extends BaseQuickAdapter<CommentResult, DynamicCommentAdapter.ViewHolder> {

    private OnCommentEvent mEvent;
    private OnCommentDataLoader onCommentDataLoader;
    //private DynamicCommentChildAdapter mChildAdapter;

    public void setOnComment(OnCommentEvent event) {
        this.mEvent = event;
    }

    public void setOnCommentDataLoader(OnCommentDataLoader loader) {
        this.onCommentDataLoader = loader;
    }


    public DynamicCommentAdapter() {
        super(R.layout.item_comment, new ArrayList<>());
    }

    @Override
    public long getItemId(int position) {
        CommentResult item = getItem(position);
        return item == null ? -1 : item.getId();
    }

    public long getLastItemId() {
        return getData().size() == 0 ? 0 : getItemId(getData().size() - 1);
    }

    @NotNull
    @Override
    protected ViewHolder createBaseViewHolder(@NotNull View view) {
        return new ViewHolder(view);
    }

    @Override
    protected void convert(DynamicCommentAdapter.ViewHolder holder, CommentResult item) {

        holder.init(getContext(), item.getReplyList());

        Glide.with(getContext())
                .load(item.getHeadImg())
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .into(holder.ivAvatar);

        holder.tvNickname.setText(item.getNickname());

        if (TimeUtils.getNowMills() < TimeUtils.string2Millis(item.getCreateTime())) {
            holder.tvTime.setText(TimeUtils.getFriendlyTimeSpanByNow(new Date()));
        } else {
            holder.tvTime.setText(TimeUtils.getFriendlyTimeSpanByNow(item.getCreateTime()));
        }

        holder.tvContent.setText(item.getCommentContent());

        //RecyclerView rvCommentChild = holder.getView(R.id.rv_comment_child);
        //rvCommentChild.setLayoutManager(new LinearLayoutManager(getContext()));
        holder.replyNum = item.getReplyNum();
        holder.replyId = item.getId();
        holder.mChildAdapter.setParentPos(holder.getAdapterPosition());

        holder.itemView.setOnClickListener(v -> {
            if (mEvent != null) {
                mEvent.onEvent(1, holder.getAdapterPosition(), -1, item);
            }
        });

    }

    class ViewHolder extends BaseViewHolder {
        ImageView ivAvatar;
        TextView tvNickname;
        TextView tvTime;
        TextView tvContent;
        LinearLayoutManager layoutManager;
        DynamicCommentChildAdapter mChildAdapter;
        RecyclerView rvChild;
        TextView tvFooter;
        int pageSize = 5;
        int replyId;
        int replyNum;

        public ViewHolder(@NotNull View view) {
            super(view);
            ivAvatar = view.findViewById(R.id.iv_avatar);
            tvNickname = view.findViewById(R.id.tv_nickname);
            tvTime = view.findViewById(R.id.tv_time);
            tvContent = view.findViewById(R.id.tv_content);
            rvChild = view.findViewById(R.id.rv_comment_child);
        }

        private void init(Context context, List<CommentResult> data) {
            layoutManager = new LinearLayoutManager(context);

            mChildAdapter = new DynamicCommentChildAdapter(data);
            rvChild.setAdapter(mChildAdapter);
            mChildAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
                @Override
                public void onChanged() {
                    super.onChanged();
                    if (mChildAdapter.getItemCount() <= 0) {
                        rvChild.setVisibility(View.GONE);
                    } else {
                        rvChild.setVisibility(View.VISIBLE);
                    }
                }
            });

            mChildAdapter.setOnItemClickListener((adapter, view1, position) -> {
                if (mEvent != null) {
                    mEvent.onEvent(2, mChildAdapter.getParentPos(), position, (CommentResult) adapter.getItem(position));
                }
            });
        }

        public void addData(List<CommentResult> data, boolean isRefresh) {
            if (isRefresh) {
                mChildAdapter.setList(data);
            } else {
                mChildAdapter.addData(data);
            }
            if (replyNum > mChildAdapter.getData().size()) {
                tvFooter = (TextView) View.inflate(getContext(), R.layout.item_comment_child, null);
                tvFooter.setTextColor(ContextCompat.getColor(getContext(), R.color.colorAccent));
                mChildAdapter.setFooterView(tvFooter);
                tvFooter.setText(String.format("查看全部%s条回复", replyNum));
                tvFooter.setOnClickListener(v -> {
                    if (onCommentDataLoader != null) {
                        onCommentDataLoader.onLoad(getAdapterPosition(), replyId, (int) mChildAdapter.getLastItemId(), pageSize);
                    }
                });
            } else {
                if (mChildAdapter.hasFooterLayout()) {
                    mChildAdapter.removeFooterView(tvFooter);
                }
            }
        }


        public void removeItem(int childPos) {
            if (mChildAdapter != null) {
                mChildAdapter.removeAt(childPos);
            }
        }

    }

 /*   public void kRemove(int type, int pos) {
        if (type == 1) {
            remove(pos);
        } else {
            removeChild(pos);
        }
    }*/

    public void removeChild(int parentPos, int pos) {
        RecyclerView rvParent = getRecyclerView();
        ViewHolder holder = (ViewHolder) rvParent.findViewHolderForAdapterPosition(parentPos);
        if (holder != null) {
            holder.removeItem(pos);
        }
    }

    public void addChildData(int parentPos, List<CommentResult> results) {
        RecyclerView rvParent = getRecyclerView();
        ViewHolder holder = (ViewHolder) rvParent.findViewHolderForAdapterPosition(parentPos);
        if (holder != null) {
            holder.addData(results, false);
        }
    }

}
